package com.agilerabbit.tools;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class GenericListDownloader {
	
	private static final String TAG_ID = "ID";
	private static final String TAG_NAME = "Title";
	private static final String TAG_DESC = "Desc";
	private static final String TAG_URL = "ImageUrl";
	private static final String TAG_FIELD1 = "Field1";
	private static final String TAG_FIELD2 = "Field2";
	private static final String TAG_LARGEIMAGE = "LargeImageUrl";
	
	private static final String TAG_IMAGE = "ImageUrl";
	
	public static List<GenericListItem> getItems(String Url)
	{
		List<GenericListItem> items = new ArrayList<GenericListItem>();
		JSONParser jParser = new JSONParser();
		JSONObject json = jParser
				.getJSONFromUrl(Url);

		String name = "list";
		JSONArray list = null;
		try {
			list = json.getJSONArray(name);
		} catch (JSONException e) {

			e.printStackTrace();
		}

		if (list != null) {
			for (int i = 0; i < list.length(); i++) {
				try {

					JSONObject c = list.getJSONObject(i);
					GenericListItem item = new GenericListItem();
					item.Title = c.getString(TAG_NAME);
					item.Desc = c.getString(TAG_DESC);
					item.ImageUrl = c.getString(TAG_URL);
					item.LargeImageUrl = c.getString(TAG_LARGEIMAGE);
					item.Field1 = c.getString(TAG_FIELD1);
					item.Field2 = c.getString(TAG_FIELD2);
					item.ID = c.getInt(TAG_ID);
					item.ImageUrl =  c.getString(TAG_IMAGE); 
					items.add(item);

					// System.out.println(item);

				} catch (JSONException e) {

					e.printStackTrace();
				}
			}
		}
		return items;
	}

}
